package com.kzd.gtgame.main.money

import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setTransitionStatusE
import com.kzd.gtgame.jetpack.vm.MoneyViewModel
import com.kzd.gtgame.main.money.adapter.MoneyExperienceAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_money_experience.*
import kotlinx.android.synthetic.main.include_app_refresh.*

class MoneyExperienceActivity : OwnerViewModeActivity<MoneyViewModel>() {

    private val mMoneyExperienceAdapter = MoneyExperienceAdapter()

    override fun viewModel(): MoneyViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_money_experience

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = flMoneyExperienceBar)
    }

    override fun initView() {

        rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mMoneyExperienceAdapter

        mSmartRefreshLayout.setOnRefreshListener {
            viewModel.moneyExperience()
        }
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.moneyExperience(isLoadMore = true)
        }
    }

    override fun initData() {
        viewModel.moneyExperience()
    }

    override fun initViewMode() {
        viewModel.moneyExperienceLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response,mMoneyExperienceAdapter,mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?:return@observe
            if (isLoadMore) {
                mMoneyExperienceAdapter.addData(data)
            } else {
                mMoneyExperienceAdapter.setNewInstance(data)
            }
        })
    }
}